(function() {
	require(['jquery', 'box', 'form', 'bootstrapTable', 'bootstrapTableZh'],function(jquery, box) {
		/*禁止滚动条*/
		if($('li:first').hasClass('selected') == true){
			$('html').css('overflow','hidden');
			$('body').css('overflow','hidden');
		}else{
			$('html').css('overflow','scroll');
			$('body').css('overflow','hidden');
		}
		
		var $table = $('#pro_notice_retrieve');
		
		function initTable() {//加载内容
			$table.bootstrapTable({
				width: '90%',
				columns: [
	      			{width: '25%', field: 'noticeName', title: '公告主题', align: 'center'},
	      			{width: '25%', field: 'content', title: '公告内容', align: 'center'},
	      			{width: '20%', field: 'publishTime', title: '发布时间', align: 'center'},
	      			{width: '10%', field: 'attachmentUrl', title: '附件', align: 'center', formatter: fundTable},
	      			{width: '5%', field: 'clickNum', title: '点击量', align: 'center'},
	      			/*{width: '5%', field: 'downNum', title: '下载量', align: 'center'},*/
	      			{width: '10%', field: 'crud', title: '操作', align: 'center', events: orgEvents, formatter: operateFormatter}]
	        });
	        
			setTimeout(function () {
	            $table.bootstrapTable('resetView');
	        }, 200);
	        
	        $table.on('expand-row.bs.table', function (e, index, row, $detail) {
	        	var detail = detailFormatter(index, row);
	        	$detail.html(detail);
	        });
	        $table.on('all.bs.table', function (e, name, args) {
	            console.log(name, args);
	        });
	    }
		
		function getIdSelections() {
	        return $.map($table.bootstrapTable('getSelections'), function (row) {
	            return row.id;
	        });
	    }
	    function responseHandler(res) {
	        $.each(res.rows, function (i, row) {
	            row.state = $.inArray(row.id, selections) !== -1;
	        });
	        return res;
	    }
	    
	    function fundTable(value, row, index) {
	    	if(row.attachmentUrl == null || row.attachmentUrl == ""){
	    		return '暂无附件'
	    	}else{
	    		return '<a href="'+ row.attachmentUrl +'" target="view_window">下载</a>';
	    	}
	    }
	    
	    /*table中  “+” 按钮添加内容*/
	    function detailFormatter(index, row) {
	        var html = [], Updater = "", updateTime = "";
	        if(row.updatetime == null || row.updater == null){
	        	Updater = "无";
	        	updateTime = "无";
	        }else {
		    	Updater = row.updater;
		    	updateTime = new Date(row.updatetime).toLocaleString();
	        }
            html.push('<p><b>创建者 :</b> ' + row.creater + '</p>'+
            		'<p><b>创建时间 :</b> ' + new Date(row.createtime).toLocaleString() + '</p>'+
            		'<p><b>更新者 :</b> ' + Updater + '</p>'+
            		'<p><b>更新时间:</b> ' + updateTime + '</p>');
	        return html.join('');
	    }
	    
	    function operateFormatter(value, row, index) {
	    	
	        return [
                '<a class="check" href="javascript:void(0)" title="查看详情">',
	            '<i class="glyphicon glyphicon-eye-open"></i>',
	            '</a>  '
	        ].join('');
	    }
	    
	    window.orgEvents = {
	       
	        'click .check': function (e, value, row, index) {
				var modalObj = box.detailModal('subContent', "公告"),
				modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId, btnSure = modalObj.btnSure;
				var detailUrl = "system/getPage.service?path=businessManage/notice/noticeDetail",
				_target = modalBodyId, _selector= "#notice_detail";
				orange.loadPage({url: detailUrl, target: _target, selector: _selector, success: function(response){
					var modal = $('#' + modalId), 
						btn_sure = $('#' + btnSure);
					$('#subContent').data('row', row);
					modal.modal({backdrop: "static"});
					//判断是否有附件
					var detailModal = '';
					if(row.attachmentUrl == null || row.attachmentUrl == ""){
						detailModal = '<strong>'+ row.noticeName +'</strong>'+
						'<p style = "border: 1px solid #aaa;border-radius: 5px;padding: 5px;min-height: 200px;margin-top: 20px;">'+row.content+'</p>'+
				        '<p style="height: 50px;margin-top: 15px;">附件：无';
					}else{
						detailModal = '<strong>'+ row.noticeName +'</strong>'+
						'<p style = "border: 1px solid #aaa;border-radius: 5px;padding: 5px;min-height: 200px;margin-top: 20px;">'+row.content+'</p>'+
				        '<p style="height: 50px;margin-top: 15px;">附件：'+
				        '<a href="'+ row.attachmentUrl +'" target="view_window">'+ row.noticeName +'</a></p>';
					}
					
					$('#notice_detail').append(detailModal);
					var code = orange.getAppContext('USER')['username'];
					
					btn_sure.unbind('click').bind('click', function() {
						modal.modal('hide');
						$.ajax({
							url:"notice/updateNoticeClickNumAndState.service",
							type: 'post',
							data:{
								noticeId:row.id,
								code:code
							},
							success: function(data) {
								ajaxRequest();
								$.ajax({
									url: "notice/retrieveNoReadNoticeByOrgId.service",
									type: "post",
									success: function(data) {
										if(data && data.success) {
											if(data.data > 0){
												$('.user-header').find('.fa-bell').addClass('blink');
												$('.user-header').find('.fa-bell').attr('title','您有'+ data.data +'条未读公告！');
											}else if(data.data == 0){
												$('.user-header').find('.fa-bell').removeClass('blink');
												$('.user-header').find('.fa-bell').attr('title','暂时没有公告！');
											}
										}
									}
								});
							}
						});
					});
				}});
	        }
	    };
	    
	    function ajaxRequest() {
	    	$.ajax({
				url:"notice/retiveNoticesByOrgId.service",
				type: 'post',
				success: function(data) {
					$table.bootstrapTable('load', data.data);
				}
			});
	    }
		
	 initTable();
	 ajaxRequest();
	    
	});
})();
